#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "canvas.h"
#include "map/mapview.h"
#include "terraria/terramodel.h"
#include <QMenu>
#include <QAction>
#include <QActionGroup>
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 设置标题
    setWindowTitle("TerraGrid");

    // 设置中心控件
    canvas = new Canvas(this);
    setCentralWidget(canvas);

    /*
     * 菜单栏
     */

    // 地图管理
    QMenu* mapMenu = ui->menubar->addMenu("地图");
    QAction* loadMapAction = mapMenu->addAction("载入");
    connect(loadMapAction, &QAction::triggered, this, [=]{ map_view->load(); });

    // 材料选择
    QMenu* materialMenu = ui->menubar->addMenu("材料");

    QAction* rockAction = materialMenu->addAction("岩石");
    rockAction->setCheckable(true);
    rockAction->setChecked(true);
    rockAction->setData(0);

    QAction* metalAction = materialMenu->addAction("金属");
    metalAction->setCheckable(true);
    metalAction->setData(1);

    QAction* concreteAction = materialMenu->addAction("混凝土");
    concreteAction->setCheckable(true);
    concreteAction->setData(2);

    QAction* gravelAction = materialMenu->addAction("砂砾");
    gravelAction->setCheckable(true);
    gravelAction->setData(3);

    QAction* hardSandAction = materialMenu->addAction("硬质沙");
    hardSandAction->setCheckable(true);
    hardSandAction->setData(4);

    QAction* soilAction = materialMenu->addAction("泥土");
    soilAction->setCheckable(true);
    soilAction->setData(5);

    QAction* softSandAction = materialMenu->addAction("软质沙");
    softSandAction->setCheckable(true);
    softSandAction->setData(6);

    QAction* mudAction = materialMenu->addAction("泥浆");
    mudAction->setCheckable(true);
    mudAction->setData(7);

    QActionGroup* materialGroup = new QActionGroup(this);
    materialGroup->setExclusive(true);
    materialGroup->addAction(rockAction);
    materialGroup->addAction(metalAction);
    materialGroup->addAction(concreteAction);
    materialGroup->addAction(gravelAction);
    materialGroup->addAction(hardSandAction);
    materialGroup->addAction(soilAction);
    materialGroup->addAction(softSandAction);
    materialGroup->addAction(mudAction);

    connect(materialGroup, &QActionGroup::triggered, this, [](QAction* action){
        unsigned material = action->data().toUInt();
        terra_model->setMaterial(material);
    });

    // 地图管理
    QMenu* fileMenu = ui->menubar->addMenu("地形");
    QAction* loadTerraAction = fileMenu->addAction("载入");
    QAction* saveTerraAction = fileMenu->addAction("保存");

    connect(loadTerraAction, &QAction::triggered, this, [this] {
        QString file_path = QFileDialog::getOpenFileName(this, "载入地形", QDir::homePath(), "Text Files (*.txt);;All Files (*)");

        if (!file_path.isEmpty())
        {
            terra_model->loadTerra(file_path);
        }
    });
    connect(saveTerraAction, &QAction::triggered, this, [this]{
        QString file_path = QFileDialog::getSaveFileName(this, "保存地形", QDir::homePath(), "Text Files (*.txt);;All Files (*)");

        if (!file_path.isEmpty())
        {
            terra_model->saveTerra(file_path);
        }
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}
